<template>
  <div class="rules-page">
    <el-button class="back-btn" text circle @click="goBack">
      <el-icon><ArrowLeft /></el-icon>
    </el-button>

    <div class="page-header">
      <h2>规则引擎</h2>
      <div class="actions">
        <el-input v-model="query.ruleCode" placeholder="规则编码" style="width:180px;margin-right:8px" clearable />
        <el-input v-model="query.ruleName" placeholder="规则名称" style="width:180px;margin-right:8px" clearable />
        <el-button type="primary" :loading="loading" @click="loadData">查询</el-button>
      </div>
    </div>

    <el-table :data="records" v-loading="loading" border style="width: 100%">
      <el-table-column prop="ruleCode" label="规则编码" min-width="160" />
      <el-table-column prop="ruleName" label="规则名称" min-width="160" />
      <el-table-column prop="ruleTypeDesc" label="类型" width="120" />
      <el-table-column prop="priority" label="优先级" width="100" />
      <el-table-column prop="statusDesc" label="状态" width="120" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button type="primary" link @click="viewRule(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pager">
      <el-pagination
        background
        layout="prev, pager, next, jumper, ->, total"
        :current-page="query.pageNum"
        :page-size="query.pageSize"
        :total="total"
        @current-change="onPageChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getRules } from '@/assets/api/mobile'
import { ArrowLeft } from '@element-plus/icons-vue'

const router = useRouter()
const loading = ref(false)
const records = ref([])
const total = ref(0)
const query = reactive({
  ruleCode: '',
  ruleName: '',
  pageNum: 1,
  pageSize: 10
})

const normalizePage = (res) => {
  const data = res?.data?.data || res?.data || {}
  return {
    records: Array.isArray(data?.records) ? data.records : (Array.isArray(data?.list) ? data.list : []),
    total: Number(data?.total || 0),
    current: Number(data?.current || data?.pageNum || 1),
    size: Number(data?.size || data?.pageSize || 10)
  }
}

const loadData = async () => {
  loading.value = true
  try {
    const res = await getRules({
      ruleCode: query.ruleCode || undefined,
      ruleName: query.ruleName || undefined,
      pageNum: query.pageNum,
      pageSize: query.pageSize
    })
    const page = normalizePage(res)
    records.value = page.records
    total.value = page.total
  } finally {
    loading.value = false
  }
}

const onPageChange = (p) => {
  query.pageNum = p
  loadData()
}

const goBack = () => router.push('/param-engine/dashboard')
const viewRule = (row) => {
  console.log('rule detail', row)
}

onMounted(loadData)
</script>

<style scoped>
.rules-page { padding: 12px; position: relative; }
.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; padding-left: 44px; position: relative; }
.pager { margin-top: 12px; text-align: right; }
.back-btn { position: absolute; top: 8px; left: 8px; z-index: 10; }
</style>



